package com.faf.ui;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.bean.Detail;
import com.faf.bean.Food;
import com.faf.dao.impl.DetailDaoImpl;
import com.faf.dao.impl.FoodDaoImpl;
import com.faf.dao.impl.OrdersDaoImpl;

/**
 * Servlet implementation class MyCart
 */
@WebServlet("/MyCart")
public class MyCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8"); 
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		FoodDaoImpl foodDao = new FoodDaoImpl();
		DetailDaoImpl detailDao = new DetailDaoImpl();
		OrdersDaoImpl ordersDao = new OrdersDaoImpl();
		
		HashMap<Integer, Integer> global_info_cart_id2count = (HashMap<Integer, Integer>)request.getSession().getAttribute("global_cart");
		
		HashMap<Integer, ArrayList<Integer>> hash_store2food = new HashMap<Integer, ArrayList<Integer>>();
		HashMap<Integer, ArrayList<Integer>> hash_store2foodcount = new HashMap<Integer, ArrayList<Integer>>();
		// 根据商家分类
		for(HashMap.Entry<Integer, Integer> entry : global_info_cart_id2count.entrySet()){
			try {
				Food food = foodDao.getFoodByFoodID(entry.getKey());
				if(!hash_store2food.containsKey(food.getStoreId())) {
					ArrayList<Integer> list = new ArrayList<Integer>();
					hash_store2food.put(food.getStoreId(), list);
				}
				if(!hash_store2foodcount.containsKey(food.getStoreId())) {
					ArrayList<Integer> list = new ArrayList<Integer>();
					hash_store2foodcount.put(food.getStoreId(), list);
				}
				hash_store2food.get(food.getStoreId()).add(food.getFoodId());
				hash_store2foodcount.get(food.getStoreId()).add(entry.getValue());
				System.out.println(hash_store2food.get(food.getStoreId()).size());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		HashMap<Integer, Food> hash_fid2food = new HashMap<Integer, Food>();
		
		// 开始存储临时数据
		for(HashMap.Entry<Integer, ArrayList<Integer>> entry : hash_store2food.entrySet()) {
			ArrayList<Integer> arrlist = entry.getValue();
			for(int foodid : arrlist) {
				try {
					if(!hash_fid2food.containsKey(foodid))
						hash_fid2food.put(foodid, foodDao.getFoodByFoodID(foodid));
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		request.setAttribute("hash_fid2food", hash_fid2food);
		request.setAttribute("hash_store2food", hash_store2food);
		request.setAttribute("hash_store2foodcount", hash_store2foodcount);
		
		request.getRequestDispatcher("/UserGlobal/cart.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
